<template>
  <div class="boxs">
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <div slot="header">
        <span>员工批量导入上传</span>
      </div>
      <!-- card body -->
      <UploadExcel :on-success="handleSuccess" :before-upload="beforeUpload" />
    </el-card>
  </div>
</template>

<script>
// 导入工具方法
import { transformKeys } from "@/utils/index.js";
// 导入上传员工的后台接口方法
import {importEmployeesApi} from '@/api/employees'
export default {
  methods: {
    // 校验本地选择Excel文件的大小
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1;

      if (isLt1M) {
        // 1.校验通过->上传的excel文件大小 < 1M(1024kb)
        return true;
      }

      this.$message({
        message: "你选择的excel文件大小超过1M了，无法上传！",
        type: "warning",
      });
      // 2.校验不通过
      return false;
    },
    // 校验通过后执行
    async handleSuccess({ results, header }) {
      // console.log(results, header)
      // 1.中英转换对照表
      const maps = {
        入职日期: "timeOfEntry",
        聘用形式: "formOfEmployment",
        手机号: "mobile",
        姓名: "username",
        转正日期: "correctionTime",
        工号: "workNumber",
        部门: "departmentName",
      };
      const data = transformKeys(results, maps, [
        "timeOfEntry",
        "correctionTime",
      ]);
     // console.log(data);
     await importEmployeesApi(data)
     this.$router.push('/employees')
    },
  },
};
</script>

<style lang="scss" scoped>
</style>